//
//  PrivacyPolicyPage.swift
//  chat
//
//  Created by 1 on 2023/4/1.
//

import SwiftUI

struct LinkString: Identifiable {
    let url: String
    var id: String { url } // or let id = UUID()
}

struct PrivacyPolicyPage: View {
    @AppStorage("userAgreePrivacyPolicy") var userAgreePrivacyPolicy: Bool = false
    @State private var linkPage: LinkString? = nil
    
    var body: some View {
        
        ZStack{
            Rectangle()
                .foregroundColor(Color(.black).opacity(0.4))
                .edgesIgnoringSafeArea(.vertical)
            
            VStack(spacing:16){
                Text("用户协议及隐私政策")
                    .font(.system(size: 18).bold())
                    .foregroundColor(MBAColor.com_white.color)

                Text("感谢您信任并使用FastAI。依据最新法律要求，我们更新了[《用户协议》](a)及[《隐私政策》](b)，特向您推送本提示。请您仔细阅读并充分理解我们对您个人信息处理原则。")
                    .font(.system(size: 13))
                    .foregroundColor(MBAColor.com_white.color)
                    .environment(\.openURL, OpenURLAction { url in
                      switch url.absoluteString {
                      case "a":
                          linkPage = LinkString(url: kAIProtocol)
                          return .handled
                      case "b":
                          //todo something
                          linkPage = LinkString(url: kAIprivacy)
                          return .handled
                        default:
                          // Intent not recognized.
                          return .discarded
                      }
                    })
                Text("如您点击“同意”，即表示您已阅读并同意更新后的协议及政策。我们将严格按照经您同意的各项条款使用您的个人信息，以便为您提供更好的服务。")
                    .font(.system(size: 13))
                    .foregroundColor(MBAColor.com_white.color)
                    .padding(.top,8)
                
                Button {
                    userAgreePrivacyPolicy = true
                } label: {
                    Text("同意")
                        .frame(height: 44)
                        .mainGradientTitle()
                        .cornerRadius(22)
                }
                Button {
                    exit(0)
                } label: {
                    Text("不同意")
                        .font(.system(size: 13))
                        .foregroundColor(MBAColor.com_white.color)
                }

            }
            .padding(.horizontal,24)
            .padding(.top,20)
            .padding(.bottom,24)
            .frame(width: 280)
            .background(MBAColor.com_background.color)
            .cornerRadius(16)
        }.frame(minWidth: 0,maxWidth: .infinity,minHeight: 0,maxHeight: .infinity)
            .background(MBAColor.com_background.color)
            .sheet(item: $linkPage, content: { linkPage in
                SafariView(url: URL(string: linkPage.url)!)
            })
        
    }
}

struct PrivacyPolicyPage_Previews: PreviewProvider {
    static var previews: some View {
        PrivacyPolicyPage()
    }
}
